package cn.wolfcode.luowowo.web.controller;

import cn.wolfcode.luowowo.annotion.RequireLogin;
import cn.wolfcode.luowowo.annotion.UserParam;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVORedisService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.StrategyComment;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.query.StrategyQuery;
import cn.wolfcode.luowowo.service.IStrategyCommentService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.IStrategyThemeService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("strategies")
public class StrategiesController {
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCommentService commentService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;
    /**
     * 旅游攻略的数据
     * @return
     */
    @GetMapping("themes")
    public Object themes(){
        return JsonResult.success(strategyThemeService.list());
    }

    /**
     * 旅游攻略的分页
     * @return
     */
    @GetMapping("query")
    public Object query(StrategyQuery qo){
        Page<Strategy> page = strategyService.query(qo);
        return JsonResult.success(page);
    }
    //攻略明细
    @GetMapping("detail")
    public Object detail(String id){
        //阅读数
        strategyStatisVORedisService.addViewnum(id,1);
        return JsonResult.success(strategyService.get(id));
    }
    /**
     * 评论回显
     * @param qo
     * @return
     */
    @GetMapping("comments")
    public Object comments(StrategyCommentQuery qo){
        return JsonResult.success(commentService.query(qo));
    }

    /**
     * 数据回显
     * @param sid
     * @return
     */
    @GetMapping("statisVo")
    public Object statisVo(String sid){

        return JsonResult.success(strategyStatisVORedisService.getStrategyStatisVo(sid));
    }

    /**
     * 添加评论操作
     * @param strategyComment
     * @return
     */
    @RequireLogin //登录判断
    @PostMapping("/addComment")
    public Object addComment(StrategyComment strategyComment, HttpServletRequest request){
        //获取token
        String token = request.getHeader("token");
        //通过token获取当前登录用户,以此来判断是否登录
        UserInfo user = userInfoRedisService.getUserByToken(token);
        //属性拷贝, 参数1:源数据,    参数2:目标数据对象
        //原理: 通过内省机制进行比对两个对象中的数据,如果目标对象有与原数据相同的字段,则进行赋值
        BeanUtils.copyProperties(user,strategyComment);
        //只有id不同
        strategyComment.setUserId(user.getId());
        //且创建时间也需要设置
        strategyComment.setCreateTime(new Date());
        commentService.save(strategyComment);//然后再进行保存操作

        //评论+1
        strategyStatisVORedisService.addReplynum(strategyComment.getStrategyId());
        return JsonResult.success();
    }

    /**
     * 点赞功能
     * @param cid
     * @return
     */
    @RequireLogin//登录判断
    @PostMapping("commentThumb")
    public Object commentThumb(String cid,HttpServletRequest request){
        String token = request.getHeader("token");
        UserInfo user = userInfoRedisService.getUserByToken(token);
        commentService.commentThumb(cid,user.getId());
        return JsonResult.success();
    }
    @RequireLogin
    @GetMapping("/info")
    public Object info(@UserParam UserInfo userInfo){
        return JsonResult.success(userInfo);
    }
    //新的需求:修改个人信息
    //新的问题: 如果是前端页面需要修改用户信息,使用用户对象来接收传入参数
    //此时仍然使用UserInfo形式参数来处理怎么办
    @RequireLogin
    @GetMapping("/info2")
    public Object info2(UserInfo userInfo){//参数接收
        return JsonResult.success(userInfo);
    }

    //旅游攻略收藏
    @RequireLogin
    @PostMapping("/favor")
    public Object favor(String sid,@UserParam UserInfo userInfo){
        //true-->收藏成功   false-->取消收藏
        boolean ret = strategyStatisVORedisService.favor(sid,userInfo.getId());
        return JsonResult.success(ret);
    }
    //顶顶功能
    @RequireLogin
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid,@UserParam UserInfo userInfo){
        //true-->顶顶成功   false-->今天已经顶过了
        boolean ret = strategyStatisVORedisService.strategyThumbup(sid,userInfo.getId());
        return JsonResult.success(ret);
    }
}
